package com.miaowang.salad.activity;

import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.activity.MapOverlayActivity.MyOverlay;
import prj.carmap.mobile.view.ZoomControlView;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.jobqueue.GetRescueLocationJob;
import com.miaowang.salad.jobqueue.JobEvents;
import com.miaowang.salad.util.CheckUtil;
import com.path.android.jobqueue.Job;

import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * @author weihe 救援车辆地图页面
 */
public class RescueCarMapActivity extends BaseActivity {

	Button btn_map_refresh;
	private ProgressDialog pd;
	MapView mMapView;
	private String id;
	private MapController mMapController = null;
	private ZoomControlView mZoomControlView;
	private MyOverlay mOverlay = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		id = getIntent().getStringExtra("id");
		btn_map_refresh = (Button) findViewById(R.id.btn_map_refresh);
		mMapView = (MapView) findViewById(R.id.bmapView);
		initControl();
		refreshMap("", "");
		getRescueLocationFromServer();
	}

	private void initControl() {
		btn_map_refresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getRescueLocationFromServer();
			}
		});

	}

	private void getRescueLocationFromServer() {
		if (pd == null) {
			pd = new ProgressDialog(RescueCarMapActivity.this);
		}
		pd.setTitle("正在获取救援工位置信息...");
		pd.show();

		Job job = new GetRescueLocationJob(id);
		CarmapApplication.getInstance().getJobManager().addJobInBackground(job);
	}

	private void refreshMap(String latitude, String longitude) {
		/**
		 * 获取地图控制器
		 */
		mMapController = mMapView.getController();
		/**
		 * 设置地图是否响应点击事件 .
		 */
		mMapController.enableClick(true);
		/**
		 * 设置地图缩放级别
		 */
		mMapController.setZoom(14);
		/**
		 * 显示内置缩放控件
		 */
		mMapView.setBuiltInZoomControls(false);

		mZoomControlView = (ZoomControlView) findViewById(R.id.OverlayZoomControlView);
		mZoomControlView.setMapView(mMapView);
		try {
			double lat = Double.parseDouble(latitude);
			double lon = Double.parseDouble(longitude);
			// String name = getIntent().getStringExtra("name");
			initDefault((int) (lat * 1E6), (int) (lon * 1E6), "");
			GeoPoint p = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
			mMapController.setCenter(p);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void initDefault(double latitude, int longitude, String name) {
		GeoPoint p2 = new GeoPoint((int) (latitude), (int) (longitude));
		OverlayItem item2 = new OverlayItem(p2, "我的位置", "");
		item2.setMarker(getResources().getDrawable(
				R.drawable.navi_map_gps_locked));
		mOverlay.addItem(item2);
		/**
		 * 将overlay 添加至MapView中
		 */
		mMapView.getOverlays().add(mOverlay);
		/**
		 * 刷新地图
		 */
		mMapView.refresh();

	}

	// EventsBus
	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobSuccess e) {
		if (e.getJobCode() == JobEvents.GET_RESCUE_LOCATION) {
			if (pd != null) {
				pd.dismiss();
			}
			OkHttpResponseData msg = (OkHttpResponseData) e.getResult();
			if (msg != null) {
				if (msg.getCode() == 0) {
					String data = msg.getData();
					if (!CheckUtil.isEmpty(data)) {
						JSONObject json;
						String latitude = null;
						String longitude = null;
						try {
							json = new JSONObject(data);
							if (json.has("latitude")) {
								latitude = json.getString("latitude");
							}
							if (json.has("longitude")) {
								longitude = json.getString("longitude");
							}
							refreshMap(latitude, longitude);
						} catch (JSONException e1) {
							e1.printStackTrace();
							Toast.makeText(RescueCarMapActivity.this, "网络请求异常",
									Toast.LENGTH_SHORT).show();
						}
					} else {
						Toast.makeText(RescueCarMapActivity.this, msg.getMsg(),
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(RescueCarMapActivity.this, msg.getMsg(),
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	@Subscribe(threadMode = ThreadMode.MainThread)
	public void onEventMainThread(JobEvents.JobFailure e) {
		if (e.getJobCode() == JobEvents.GET_RESCUE_LOCATION) {
			if (pd != null) {
				pd.dismiss();
			}
			Toast.makeText(RescueCarMapActivity.this, "网络请求异常",
					Toast.LENGTH_SHORT).show();
		}
	}
}
